import unittest, requests

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from util import parser_html, read_json
from api.api_approve_trust import ApiApproveTrust


class TestTenderList(unittest.TestCase):
    # 初始化
    def setUp(self) -> None:
        # 获取session
        self.session = requests.session()
        # 获取ApiRegisterLogin对象
        self.reg = ApiRegisterLogin(self.session)
        # 获取ApiApproveTrust对象
        self.approve = ApiApproveTrust(self.session)
        # 获取ApiTender对象
        self.tender = ApiTender(self.session)

    # 结束
    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    def test01_tender_list(self):
        phone = "15036420000"
        img_code = 8888
        pwd = "test123"
        phone_code = 666666
        card_id = "110101199006078430 "
        # 1、获取图片验证码
        self.reg.api_img_code(123123)
        # 2、获取短信验证码
        self.reg.api_phone_code(phone, img_code)
        # 3、注册
        self.reg.api_register(phone, pwd, img_code, phone_code)
        # 4、登录
        self.reg.api_login(phone, pwd)
        # 5、认证
        self.approve.api_approve(card_id)
        # 6、请求后台开户
        r = self.approve.api_trust()
        # 7、三方开户
        result = parser_html(r)
        # 期望 (http:xxx,{"Version":10,})
        r = self.session.post(url=result[0], data=result[1])
        print("三方开户的结果为:", r.text)
        self.assertIn("OK", r.text)
        # 8、获取图片验证码
        self.approve.api_img_code(123123)
        # 9、后台充值
        r = self.approve.api_recharge(img_code)
        # 10、三方充值
        result = parser_html(r)
        # 期望 (http:xxx,{"Version":10,})
        r = self.session.post(url=result[0], data=result[1])
        print("三方充值的结果为:", r.text)
        self.assertIn("OK", r.text)
        # 11、请求后台投资
        r = self.tender.api_tender(100)
        # 12、三方投资
        result = parser_html(r)
        # 期望 (http:xxx,{"Version":10,})
        r = self.session.post(url=result[0], data=result[1])
        print("三方投资的结果为:", r.text)
        self.assertIn("OK", r.text)
